local qiebing = fk.CreateSkill{
  name = "n_qiebing",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["n_qiebing"] = "窃柄",
  [":n_qiebing"] = "锁定技，你获得本技能时，令所有角色获得“蠹蚀”；当一名角色死亡时，若其没有手牌，你选择其一个技能获得。",

  ["#qiebing-invoke"] = "窃柄：选择 %dest 的一个技能获得",
}

qiebing:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player)
    return player:hasSkill(qiebing.name)
  end,
  on_use = function(self, event, target, player)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:handleAddLoseSkills(p, "n_dushi", nil, false, true)
    end
  end,
})

qiebing:addAcquireEffect(function(self, player, is_start)
  if not is_start then
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:handleAddLoseSkills(p, "n_dushi", nil, false, true)
    end
  end
end)

--[[
qiebing:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    if player:hasSkill("n_dushi") and table.find(Fk:currentRoom().alive_players, function(p)
      return p:hasSkill(qiebing.name)
    end) then
      return true
    end
  end,
})
--]]

qiebing:addEffect(fk.Death, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qiebing.name) and target:isKongcheng() then
      local choices = {}
      for _, skill_name in ipairs(target:getSkillNameList()) do
        local skill = Fk.skills[skill_name]
        if not player:hasSkill(skill, true) and not skill:hasTag(Skill.Wake) and
          not skill:hasTag(Skill.Lord) then
          table.insertIfNeed(choices, skill_name)
        end
      end
      if #choices > 0 then
        event:setCostData(self, {extra_data = choices})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local skills = event:getCostData(self).extra_data
    local result = room:askToCustomDialog(player, {
      skill_name = qiebing.name,
      qml_path = "packages/utility/qml/ChooseSkillBox.qml",
      extra_data = {
        skills, 0, 1, "#qiebing-invoke::"..target.id,
      },
    })
    if result ~= "" then
      event:setCostData(self, {tos = {target}, choice = json.decode(result)[1]})
    else
      event:setCostData(self, {tos = {target}, choice = table.random(skills)})
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    room:handleAddLoseSkills(player, choice)
  end,
})

return qiebing
